<template>
  <el-dialog :title="isEdit?'修改定时任务':'新增定时任务'" :visible.sync="visible"
             @close="$emit('update:show', false)">
    <el-form label-width="120px" :rules="rules" :model="params" ref="editForm" size="small">
      <el-form-item label="任务名称" prop="jobName">
        <el-input placeholder="请输入任务名称" v-model="params.jobName"></el-input>
      </el-form-item>
      <el-form-item label="任务描述" prop="description">
        <el-input placeholder="请输入任务描述" v-model="params.description"></el-input>
      </el-form-item>
      <el-form-item label="执行时间" prop="cronExpression">
        <el-input placeholder="请输入执行时间" v-model="params.cronExpression"></el-input>
      </el-form-item>
      <el-form-item label="任务分组" prop="jobGroup">
        <el-input placeholder="请输入任务分组" v-model="params.jobGroup"></el-input>
      </el-form-item>
      <el-form-item label="执行类" prop="jobClassName">
        <el-input placeholder="请输入执行类" v-model="params.jobClassName"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="saveEdit">保 存</el-button>
    </div>
  </el-dialog>
</template>
<script>

  export default {
    name: "AddEditMaterialDialog",
    components: {},
    props: {
      show: {
        type: Boolean,
        default: false
      },
      timerData: null
    },
    watch: {
      show() {
        this.visible = this.show;
        if (!this.timerData) {
          for (let k in this.params) {
            this.params[k] = '';
          }
          this.isEdit = false;
        }
      },
      timerData: {
        handler() {
          if (this.timerData) {
            for (let k in this.params) {
              // debugger
              if (k == "oldJobName") {
                this.params[k] = this.timerData["jobName"];
              }
              else if (k == 'oldJobGroup') {
                this.params[k] = this.timerData["jobGroup"];
              }
              else {
                this.params[k] = this.timerData[k] ? this.timerData[k] : null;
              }
            }
            this.isEdit = true;
          }
        },
        immediate: true,
        deep: true
      }
    },
    data() {
      return {
        visible: this.show,
        isEdit: false,
        params: {
          id: '',
          jobName: '',
          description: '',
          cronExpression: '',
          jobGroup: '',
          jobClassName: '',
          oldJobName: '',
          oldJobGroup: ''
        },
        rules: {
          jobName: [
            {required: true, message: '任务名称不能为空', trigger: 'blur'}
          ],
          cronExpression: [
            {required: true, message: '执行时间不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      saveEdit: function () {
        this.$refs['editForm'].validate((valid) => {
          if (valid) {
            let _url = '/app-sync-data/timer/job/add';
            this.api.post(_url, this.params)
              .then((data) => {
                if (data.code === 200) {
                  this.visible = false;
                  this.$message({
                    message: '保存成功',
                    type: 'success'
                  });
                  this.$parent.query();
                } else {
                  this.$message.error(data.data);
                }
              });
          } else {
            return false;
          }
        });
      }
    }
  }
</script>
